<?php

declare(strict_types=1);

namespace app\common\model;

use think\facade\Cache;


class PreceptEvaluate extends Base

{
    protected $name = 'precept_evaluate';
    /**
     * 获取评价内容
     */
    public static function getComment()
    {
        $list = [
            [
                'id' => 1,
                'title' => '流程是否顺畅',
                'son' => self::geticon()
            ],
            [
                'id' => 2,
                'title' => '服务方式是否完善',
                'son' => self::geticon()
            ],
            [
                'id' => 3,
                'title' => '服务人员专业度',
                'son' => self::geticon()
            ],
            [
                'id' => 4,
                'title' => '方案效果',
                'son' => self::geticon()
            ],
            [
                'id' => 5,
                'title' => '预期目标',
                'son' => self::geticon()
            ],
        ];

        return $list;
    }
    public static function geticon()
    {
        return [
            [
                'title' => '-1分',
                'score' => -1,
                'img' => '/static/api/img/evaluate_-1.png',
                'img_select' => '/static/api/img/evaluate_select_-1.png',
                'check' => false
            ],
            [
                'title' => '0分',
                'score' => 0,
                'img' => '/static/api/img/evaluate_0.png',
                'img_select' => '/static/api/img/evaluate_select_0.png',
                'check' => false
            ],
            [
                'title' => '1分',
                'score' => 1,
                'img' => '/static/api/img/evaluate_1.png',
                'img_select' => '/static/api/img/evaluate_select_1.png',
                'check' => false
            ]
        ];
    }
    /**
     * 提交评价
     */
    public static function submit($param, $user)
    {
        try {
            $info = Precept::where(['id' => $param['id']])->find();
            if (empty($info)) {
                return ['code' => -1, 'msg' => '方案不存在'];
            }
            if ($info->status != 5) {
                return ['code' => -1, 'msg' => '方案未支付，无法评价'];
            }
            if ($info->uid != $user->id) {
                return ['code' => -1, 'msg' => '非本人方案，无法评价'];
            }
            $find = parent::where(['uid' => $user->id, 'precept_id' => $param['id']])->find();
            if ($find) {
                return ['code' => -1, 'msg' => '已评价，无法重复评价'];
            }
            if (!isset($param['data']) || empty($param['data'])) {
                return ['code' => -1, 'msg' => '参数错误'];
            }
            //json_decode(stripslashes(htmlspecialchars_decode($param['data'])),true)
            $jsondata =$param['data'];
            $data['uid'] = $user->id;
            $data['precept_id'] = $param['id'];
            foreach ($jsondata as $k => $v) {
                if (!isset($v['score']) || $v['score'] == '') {
                    return ['code' => -1, 'msg' => '评价分数不能为空'];
                }
                $data['score_' . $v['id']] = $v['score'];
            }
            $res = parent::create($data);
            $info->status = 6;
            $info->finish_time=time();
            $info->save();
            if ($res) {
                return ['code' => 1, 'msg' => '评价成功'];
            } else {
                return ['code' => -1, 'msg' => '评价失败'];
            }
        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }
}
